﻿namespace RolePlayingGame
{
    using System;
    class Menu
    {
        private static int sirStringWidth;
        private static int cardinalWidth;
        private static int speedyWidth;
        private static int middleHeight;
        private static string chooseBorder = new string('-', 12);
        private static string removeBorder = new string(' ', 12);

        private static string sirStringInfo = "A powerful paladin with excellent range damage skills but light armored.       ";
        private static string cardinalInfo = "A wise tough monk, who is slowed down by his age and heavy armor               ";
        private static string speedyInfo = "An agile wily rogue with perfect running skills but decreased damage abilities ";

        private static int[] classesActive = { 0, 1, 0 };
        // 4x10
        private static char[,] SirString = new char[,]
        {
            { 'S','i','r',' ','S','t','r','i','n','g'},
            { ' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
            { ' ',' ',' ','t','h','e',' ',' ',' ',' '},
            { ' ','F','e','a','r','l','e','s','s',' '},
        };

        private static char[,] CardinalList = new char[,]
        {
            { ' ','C','a','r','d','i','n','a','l',' '},
            { ' ',' ',' ','L','i','s','t',' ',' ',' '},
            { ' ',' ','o','f',' ','t','h','e',' ',' '},
            { 'N','u','m','e','r','a','b','l','e','s'},
        };

        private static char[,] Speedy = new char[,]
        {
            { ' ',' ','S','p','e','e','d','y',' ',' '},
            { ' ',' ',' ','t','h','e',' ',' ',' ',' '},
            { ' ',' ','S','t','r','i','n','g',' ',' '},
            { ' ','B','u','i','l','d','e','r',' ',' '},
        };

        private static void PrintClass(char[,] character, int width, int height)
        {
            Console.SetCursorPosition(width, height);
            for (int i = 0; i < character.GetLength(0); i++)
            {
                for (int j = 0; j < character.GetLength(1); j++)
                {
                    Console.Write(character[i, j]);
                }
                height++;
                Console.SetCursorPosition(width, height);
            }
        }
        // Speedy the Stringbuilder
        public static void Start()
        {
            PrintClasses();
            bool isEnterPressed = false;
            while (isEnterPressed == false)
            {
                isEnterPressed = ListenKey(isEnterPressed);
            }
            for (int i = 0; i < classesActive.Length; i++)
            {
                if (i == 1)
                {
                    Intro.playerChoose = i + 1;
                    break;
                }
            }
        }

        private static bool ListenKey(bool isEnterPressed)
        {
            var key = Console.ReadKey(true);

            if (key.Key.Equals(ConsoleKey.Enter))
            {
                isEnterPressed = true;
            }
            else if (key.Key.Equals(ConsoleKey.LeftArrow))
            {
                RemoveOldChoose();
                ChangeChooseLeft();
                VisualizeChoose();
            }
            else if (key.Key.Equals(ConsoleKey.RightArrow))
            {
                RemoveOldChoose();
                ChangeChooseRight();
                VisualizeChoose();
            }
            return isEnterPressed;
        }

        private static void VisualizeChoose()
        {
            VisualizeNewChoose();
        }

        private static void RemoveOldChoose()
        {
            for (int i = 0; i < classesActive.Length; i++)
            {
                if (classesActive[i] == 1)
                {
                    if (i == 0)
                    {
                        RemoveChoose(cardinalWidth);
                    }
                    else if (i == 1)
                    {
                        RemoveChoose(sirStringWidth);
                    }
                    else if (i == 2)
                    {
                        RemoveChoose(speedyWidth);
                    }
                    break;
                }
            }
        }

        private static void RemoveChoose(int startWidth)
        {
            Console.SetCursorPosition(startWidth - 1, middleHeight - 1);
            Console.Write(removeBorder);
            Console.SetCursorPosition(startWidth - 1, middleHeight + 4);
            Console.Write(removeBorder);
        }

        private static void VisualizeNewChoose()
        {
            for (int i = 0; i < classesActive.Length; i++)
            {
                if (classesActive[i] == 1)
                {
                    if (i == 0)
                    {
                        PrintChoose(cardinalWidth);
                        PrintInfo("cardinal");
                    }
                    else if (i == 1)
                    {
                        PrintChoose(sirStringWidth);
                        PrintInfo("sir");
                    }
                    else if (i == 2)
                    {
                        PrintChoose(speedyWidth);
                        PrintInfo("speedy");
                    }
                    break;
                }
            }
        }

        private static void PrintInfo(string p)
        {
            Console.SetCursorPosition(cardinalWidth - 10, middleHeight + 16);
            Console.ForegroundColor = ConsoleColor.Cyan;
            if (p == "speedy")
            {
                Console.Write(speedyInfo);
            }
            else if (p == "sir")
            {
                Console.Write(sirStringInfo);
            }
            else if (p == "cardinal")
            {
                Console.Write(cardinalInfo);
            }
            Console.ResetColor();
        }

        private static void PrintChoose(int startWidth)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(startWidth - 1, middleHeight - 1);
            Console.Write(chooseBorder);
            Console.SetCursorPosition(startWidth - 1, middleHeight + 4);
            Console.Write(chooseBorder);
            Console.ResetColor();
        }

        private static void ChangeChooseLeft()
        {
            for (int i = 0; i < classesActive.Length; i++)
            {
                if (classesActive[i] == 1)
                {
                    if (i == 0)
                    {
                        classesActive[classesActive.Length - 1] = 1;
                        classesActive[i] = 0;
                        break;
                    }
                    else
                    {
                        classesActive[i - 1] = 1;
                        classesActive[i] = 0;
                        break;
                    }
                }
            }
        }

        private static void ChangeChooseRight()
        {
            for (int i = 0; i < classesActive.Length; i++)
            {
                if (classesActive[i] == 1)
                {
                    if (i == classesActive.Length - 1)
                    {
                        classesActive[0] = 1;
                        classesActive[classesActive.Length - 1] = 0;
                        break;
                    }
                    else
                    {
                        classesActive[i + 1] = 1;
                        classesActive[i] = 0;
                        break;
                    }
                }
            }
        }

        private static void PrintClasses()
        {
            int middleWidth;
            CalculateMiddleHeightAndWidth(out middleWidth, out middleHeight);

            sirStringWidth = middleWidth;
            cardinalWidth = middleWidth - CardinalList.GetLength(1) - 10;
            speedyWidth = middleWidth + Speedy.GetLength(1) + 10;

            PrintClass(SirString, sirStringWidth, middleHeight);
            PrintClass(Speedy, speedyWidth, middleHeight);
            PrintClass(CardinalList, cardinalWidth, middleHeight);
        }

        private static void CalculateMiddleHeightAndWidth(out int middleWidth, out int middleHeight)
        {
            middleWidth = Console.WindowWidth / 2;
            middleWidth -= SirString.GetLength(1) / 2;
            middleHeight = (Console.WindowHeight / 2) - (SirString.GetLength(0) * 2);
        }
    }
}
